/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.HazardousChemicalsUsageRecord;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.HazardousChemicalsUsageRecordRepository;
import me.zhengjie.modules.biz.service.HazardousChemicalsUsageRecordService;
import me.zhengjie.modules.biz.service.dto.HazardousChemicalsUsageRecordDto;
import me.zhengjie.modules.biz.service.dto.HazardousChemicalsUsageRecordQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.HazardousChemicalsUsageRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class HazardousChemicalsUsageRecordServiceImpl implements HazardousChemicalsUsageRecordService {

    private final HazardousChemicalsUsageRecordRepository hazardousChemicalsUsageRecordRepository;
    private final HazardousChemicalsUsageRecordMapper hazardousChemicalsUsageRecordMapper;

    @Override
    public Map<String,Object> queryAll(HazardousChemicalsUsageRecordQueryCriteria criteria, Pageable pageable){
        Page<HazardousChemicalsUsageRecord> page = hazardousChemicalsUsageRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(hazardousChemicalsUsageRecordMapper::toDto));
    }

    @Override
    public List<HazardousChemicalsUsageRecordDto> queryAll(HazardousChemicalsUsageRecordQueryCriteria criteria){
        return hazardousChemicalsUsageRecordMapper.toDto(hazardousChemicalsUsageRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public HazardousChemicalsUsageRecordDto findById(Integer id) {
        HazardousChemicalsUsageRecord hazardousChemicalsUsageRecord = hazardousChemicalsUsageRecordRepository.findById(id).orElseGet(HazardousChemicalsUsageRecord::new);
        ValidationUtil.isNull(hazardousChemicalsUsageRecord.getId(),"HazardousChemicalsUsageRecord","id",id);
        return hazardousChemicalsUsageRecordMapper.toDto(hazardousChemicalsUsageRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HazardousChemicalsUsageRecordDto create(HazardousChemicalsUsageRecord resources) {
        return hazardousChemicalsUsageRecordMapper.toDto(hazardousChemicalsUsageRecordRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(HazardousChemicalsUsageRecord resources) {
        HazardousChemicalsUsageRecord hazardousChemicalsUsageRecord = hazardousChemicalsUsageRecordRepository.findById(resources.getId()).orElseGet(HazardousChemicalsUsageRecord::new);
        ValidationUtil.isNull( hazardousChemicalsUsageRecord.getId(),"HazardousChemicalsUsageRecord","id",resources.getId());
        hazardousChemicalsUsageRecord.copy(resources);
        hazardousChemicalsUsageRecordRepository.save(hazardousChemicalsUsageRecord);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            hazardousChemicalsUsageRecordRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<HazardousChemicalsUsageRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (HazardousChemicalsUsageRecordDto hazardousChemicalsUsageRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("危化品", hazardousChemicalsUsageRecord.getHcId());
            map.put("使用者", hazardousChemicalsUsageRecord.getUserId());
            map.put("类型", hazardousChemicalsUsageRecord.getType());
            map.put(" createdAt",  hazardousChemicalsUsageRecord.getCreatedAt());
            map.put(" updatedAt",  hazardousChemicalsUsageRecord.getUpdatedAt());
            map.put("是否删除", hazardousChemicalsUsageRecord.getIsDeleted());
            map.put("状态", hazardousChemicalsUsageRecord.getStatus());
            map.put("审批人员", hazardousChemicalsUsageRecord.getAuditorId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}